package com.jeesite.modules.dataScreen.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.jeesite.modules.dataScreen.dao.IndustryIndexDao;
import com.jeesite.modules.dataScreen.dao.RailWayDao;
import com.jeesite.modules.dataScreen.web.dto.IndustryIndexParam;
import com.jeesite.modules.dataScreen.web.vo.BasicPostalVO;
import com.jeesite.modules.dataScreen.web.vo.BasicRailwayVO;
import com.jeesite.modules.dataScreen.web.vo.IndustryIndexVo;
import com.jeesite.modules.dataScreen.web.vo.MileagePopUpVo;
import com.jeesite.modules.dataScreen.web.vo.railway.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.jeesite.modules.dataScreen.web.vo.IndustryIndexFun.fun;

@Service
public class RailWayService {

    @Autowired
    private RailWayDao railWayDao;

    @Resource
    private IndustryIndexDao industryIndexDao;

    @Resource
    private IndustryIndexService industryIndexService;

    public JSONObject getTrainDelay(String status) {

        List<TrainDelayVO> allData = railWayDao.getTrainDelay(status);
        JSONObject jsonObject = new JSONObject();

        int num = allData.stream().filter(e -> StrUtil.equals(e.getStatus(), "处置中")).mapToInt(a -> Integer.parseInt(a.getLateNumber())).sum();
        jsonObject.put("total", num);
        jsonObject.put("data", allData);
        return jsonObject;
    }

    public List<TrainDelayVO> getRetentionProcess(String name) {

        List<TrainDelayVO> trainDelayVOS = railWayDao.getRetentionProcess(name);
        List<TrainDelayVO> result = trainDelayVOS.stream().filter(trainDelayVO -> {
            if ("发生".equals(trainDelayVO.getStatus())) {
                trainDelayVO.setProcess(trainDelayVO.getEventContent());
                return true;
            }
            if ("处置中".equals(trainDelayVO.getStatus())) {
                trainDelayVO.setProcess(trainDelayVO.getDisposalMeasure());
                return true;
            }
            if ("结束".equals(trainDelayVO.getStatus())) {
                trainDelayVO.setProcess(trainDelayVO.getOtherThings());
                return true;
            }
            return true;
        }).collect(Collectors.toList());

        return result;
    }

    public TransportDataVo getTransportDataVo(String indexName1, String indexName2) {
        List<TransportDataItemVo> dataItemVos = railWayDao.getTransportData(indexName1, indexName2, DateUtil.year(DateUtil.date()));
        TransportDataVo dataVo = new TransportDataVo();
        if (CollUtil.isNotEmpty(dataItemVos)) {
            int sum = dataItemVos.stream().mapToInt(e -> {
                try {
                    return Integer.parseInt(e.getLkMonth());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return 0;
            }).sum();
            dataVo.setFsl(sum);
            sum = dataItemVos.stream().mapToInt(e -> {
                try {
                    return Integer.parseInt(e.getZzMonth());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return 0;
            }).sum();
            dataVo.setZzl(sum);
        }
        dataVo.setItemVos(dataItemVos);
        return dataVo;
    }

    public RailWayInvestVo getRailInvest() {
        RailWayInvestVo result = new RailWayInvestVo();
        Map<String, IndustryIndexVo> indexVoMap = doGetIndustryIndex("基础设施", "铁路管理", "铁路建设", "铁管处", "当年累计完成投资率", "项目总体形象进度",
                "当年铁路重点工程项目总投资", "当年铁路重点工程项目计划投资",
                "当年铁路重点工程项目里程", "当年普速铁路重点工程项目总投资", "当年高速铁路重点工程项目总投资",
                "当年普速铁路重点工程项目计划投资", "当年高速铁路重点工程项目计划投资", "当年普速铁路重点工程项目里程",
                "当年高速铁路重点工程项目里程");
        result.setInvestOKRate(indexVoMap.get("当年累计完成投资率"));
        result.setInvestSchedule(indexVoMap.get("项目总体形象进度"));
        result.setInvestTotal(indexVoMap.get("当年铁路重点工程项目总投资"));
        result.setInvestPlanTotal(indexVoMap.get("当年铁路重点工程项目计划投资"));
        result.setMileage(indexVoMap.get("当年铁路重点工程项目里程"));

        result.setInvestGs(indexVoMap.get("当年高速铁路重点工程项目总投资"));
        result.setInvestPs(indexVoMap.get("当年普速铁路重点工程项目总投资"));
        result.setInvestPlanGs(indexVoMap.get("当年高速铁路重点工程项目计划投资"));
        result.setInvestPlanPs(indexVoMap.get("当年普速铁路重点工程项目计划投资"));
        result.setGsMileage(indexVoMap.get("当年高速铁路重点工程项目里程"));
        result.setPsMileage(indexVoMap.get("当年普速铁路重点工程项目里程"));
        return result;
    }


    public RailWayMileageVo railWayMileage() {
        RailWayMileageVo result = new RailWayMileageVo();
        Map<String, IndustryIndexVo> indexVoMap = doGetIndustryIndex("基础设施", "铁路管理", "铁路基础信息", "铁管处",
                "铁路运营里程", "高速铁路里程", "普速铁路里程", "去年铁路复线里程",
                "去年铁路电气化里程", "去年国家铁路里程",
                "去年地方铁路里程", "去年客货共线里程", "去年客运专线里程", "去年货运专线里程");
        result.setOperate(indexVoMap.get("铁路运营里程"));
        result.setGs(indexVoMap.get("高速铁路里程"));
        result.setPs(indexVoMap.get("普速铁路里程"));
        result.setPluralLine(indexVoMap.get("去年铁路复线里程"));
        result.setGasify(indexVoMap.get("去年铁路电气化里程"));
        result.setCountry(indexVoMap.get("去年国家铁路里程"));
        result.setPlace(indexVoMap.get("去年地方铁路里程"));
        result.setKyzx(indexVoMap.get("去年客运专线里程"));
        result.setKhgx(indexVoMap.get("去年客货共线里程"));
        result.setHyzx(indexVoMap.get("去年货运专线里程"));
        return result;
    }


    private Map<String, IndustryIndexVo> doGetIndustryIndex(String plate, String runway, String subRunway, String liabilityUnit, String... industryNames) {
        List<String> indicatorNames = CollUtil.toList(industryNames);
        IndustryIndexParam indexParam = IndustryIndexParam.builder().plate(plate).runway(runway).subRunway(subRunway).liabilityUnit(liabilityUnit).indicatorNames(indicatorNames).build();
        return industryIndexDao.findUnRealtimeAllBy(indexParam);
    }

    public RailWayNetVo railWayNet() {
        RailWayNetVo result = new RailWayNetVo();
        Map<String, IndustryIndexVo> indexVoMap = doGetIndustryIndex("基础设施", "铁路管理", "铁路基础信息", "铁管处",
                "去年铁路路网密度", "去年高速铁路路网密度"
                , "去年普速铁路路网密度", "去年铁路复线率", "去年铁路电气化率");
        result.setTllwmd(indexVoMap.get("去年铁路路网密度"));
        result.setGslwmd(indexVoMap.get("去年高速铁路路网密度"));
        result.setPslwmd(indexVoMap.get("去年普速铁路路网密度"));
        result.setFxRate(indexVoMap.get("去年铁路复线率"));
        result.setDqhRate(indexVoMap.get("去年铁路电气化率"));
        return result;
    }

    public JSONObject railBasicService() {

        Map<String, IndustryIndexVo> indexVoMap = doGetIndustryIndex(null, null, null, null,
                "轨道运营线路条数", "轨道交通列车数量", "轨道配属车辆基地数", "轨道控制中心数", "轨道交通跨江公轨两用桥数"
                , "轨道交通跨江专用桥数", "轨道穿山隧道", "轨道主变电所数", "轨道运营车站数", "轨道地下车站数", "轨道高架车站数"
                , "轨道地面车站数", "轨道换乘车站数", "自动充值售票机可靠度", "进出站闸机可靠度", "电扶梯可靠度");

        JSONObject result = new JSONObject();
        result.put("gdyyxlts", indexVoMap.get("轨道运营线路条数"));
        result.put("gdjtlcsl", indexVoMap.get("轨道交通列车数量"));
        result.put("gdpscljds", indexVoMap.get("轨道配属车辆基地数"));
        result.put("gdkzzxs", indexVoMap.get("轨道控制中心数"));
        result.put("gdjtkjgglyqs", indexVoMap.get("轨道交通跨江公轨两用桥数"));
        result.put("gdjtkjzyqs", indexVoMap.get("轨道交通跨江专用桥数"));
        result.put("gdcssd", indexVoMap.get("轨道穿山隧道"));
        result.put("gdzbdss", indexVoMap.get("轨道主变电所数"));
        result.put("gdyyczs", indexVoMap.get("轨道运营车站数"));
        result.put("gddxczs", indexVoMap.get("轨道地下车站数"));
        result.put("gdgjczs", indexVoMap.get("轨道高架车站数"));
        result.put("gddmczs", indexVoMap.get("轨道地面车站数"));
        result.put("gdhcczs", indexVoMap.get("轨道换乘车站数"));
        result.put("zdczspjkkd", indexVoMap.get("自动充值售票机可靠度"));
        result.put("jczzjkkd", indexVoMap.get("进出站闸机可靠度"));
        result.put("dftkkd", indexVoMap.get("电扶梯可靠度"));
        return result;
    }

    public JSONObject trackMileage() {
        Map<String, IndustryIndexVo> indexVoMap = doGetIndustryIndex(null, null, null, null,
                "轨道线网运营里程", "轨道地下里程", "轨道高架里程", "轨道地面里程"
                , "轨道线网安全运营天数", "轨道线网密度", "轨道线网最小行车间隔", "轨道线网客运强度", "轨道线网最大服务时间"
                , "轨道运营事故数", "轨道运营计划开行列次", "轨道站点密度", "轨道线路客运强度", "轨道万人车站拥有率", "轨道万人线网拥有率");

        JSONObject result = new JSONObject();
        result.put("gdxwyylc", indexVoMap.get("轨道线网运营里程"));
        result.put("gddxlc", indexVoMap.get("轨道地下里程"));
        result.put("gdgjlc", indexVoMap.get("轨道高架里程"));
        result.put("gddmlc", indexVoMap.get("轨道地面里程"));
        result.put("gdxwaqyyts", indexVoMap.get("轨道线网安全运营天数"));
        result.put("gdxwmd", indexVoMap.get("轨道线网密度"));
        result.put("gdxwzxxcjg", indexVoMap.get("轨道线网最小行车间隔"));
        result.put("gdxwkyqd", indexVoMap.get("轨道线网客运强度"));
        result.put("gdxwzdfwsj", indexVoMap.get("轨道线网最大服务时间"));
        result.put("gdyysgs", indexVoMap.get("轨道运营事故数"));
        result.put("gdyyjhkxlc", indexVoMap.get("轨道运营计划开行列次"));
        result.put("gdzdmd", indexVoMap.get("轨道站点密度"));
        result.put("gdxlkyqd", indexVoMap.get("轨道线路客运强度"));
        result.put("gdwrczyyl", indexVoMap.get("轨道万人车站拥有率"));
        result.put("gdwrxwyyl", indexVoMap.get("轨道万人线网拥有率"));
        return result;
    }

    public BasicRailwayVO basicRailway() {

        BasicRailwayVO basicRailwayVO = new BasicRailwayVO();
        industryIndexService.assembly(basicRailwayVO, false, CollUtil.toList(
                fun("去年铁路运营里程", BasicRailwayVO::getQntlyylc),
                fun("高速铁路里程", BasicRailwayVO::getGstllc),
                fun("普速铁路里程", BasicRailwayVO::getPstllc),

                fun("铁路沿线安全环境治理累计整治", BasicRailwayVO::getTlyxaqhjzlljzz),
                fun("铁路沿线安全环境治理累计排查", BasicRailwayVO::getTlyxaqhjzlljpc),
                fun("去年铁路沿线安全环境治理累计整治", BasicRailwayVO::getQntlyxaqhjzlljzz),
                fun("去年铁路沿线安全环境治理累计排查", BasicRailwayVO::getQntlyxaqhjzlljpc),

                fun("当年铁路重点工程项目总投资", BasicRailwayVO::getDntlzdgcxmztz),
                fun("当年铁路重点工程项目计划投资", BasicRailwayVO::getDntlzdgcxmjhtz),
                fun("当年铁路重点工程项目里程", BasicRailwayVO::getDntlzdgcxmlc),
                fun("当年累计完成投资率", BasicRailwayVO::getDnljwctzl),
                fun("当年高速铁路重点工程项目总投资", BasicRailwayVO::getDngstlzdgcxmztz),
                fun("当年普速铁路重点工程项目总投资", BasicRailwayVO::getDnpstlzdgcxmztz),
                fun("当年高速铁路重点工程项目里程", BasicRailwayVO::getDngstlzdgcxmlc),
                fun("当年普速铁路重点工程项目里程", BasicRailwayVO::getDnpstlzdgcxmlc),
                fun("项目总体形象进度", BasicRailwayVO::getXmztxxjd),

                fun("轨道运营线路条数", BasicRailwayVO::getGdyyxlts),
                fun("轨道交通列车数量", BasicRailwayVO::getGdjtlcsl),
                fun("轨道配属车辆基地数", BasicRailwayVO::getGdpscljds),
                fun("轨道交通跨江专用桥数", BasicRailwayVO::getGdjtkjzyqs),
                fun("轨道控制中心数", BasicRailwayVO::getGdkzzxs),
                fun("轨道交通跨江公轨两用桥数", BasicRailwayVO::getGdjtkjgglyqs),
                fun("轨道穿山隧道", BasicRailwayVO::getGdcssd),
                fun("轨道主变电所数", BasicRailwayVO::getGdzbdss),

                fun("轨道运营车站数", BasicRailwayVO::getGdyyczs),
                fun("轨道地下车站数", BasicRailwayVO::getGddxczs),
                fun("轨道高架车站数", BasicRailwayVO::getGdgjczs),
                fun("轨道地面车站数", BasicRailwayVO::getGddmczs),
                fun("轨道换乘车站数", BasicRailwayVO::getGdhcczs),

                fun("自动充值售票机可靠度", BasicRailwayVO::getZdczspjkkd),
                fun("进出站闸机可靠度", BasicRailwayVO::getJczzjkkd),
                fun("电扶梯可靠度", BasicRailwayVO::getDftkkd),

                fun("轨道线网运营里程", BasicRailwayVO::getGdxwyylc),
                fun("轨道地下里程", BasicRailwayVO::getGddxlc),
                fun("轨道高架里程", BasicRailwayVO::getGdgjlc),
                fun("轨道地面里程", BasicRailwayVO::getGddmlc),

                fun("轨道线网安全运营天数", BasicRailwayVO::getGdxwaqyyts),
                fun("轨道线网密度", BasicRailwayVO::getGdxwmd),
                fun("轨道线网最小行车间隔", BasicRailwayVO::getGdxwzxxcjg),
                fun("轨道线网客运强度", BasicRailwayVO::getGdxwkyqd),
                fun("轨道线网最大服务时间", BasicRailwayVO::getGdxwzdfwsj),

                fun("轨道运营事故数", BasicRailwayVO::getGdyysgs),
                fun("轨道运营计划开行列次", BasicRailwayVO::getGdyyjhkxlc),
                fun("轨道站点密度", BasicRailwayVO::getGdzdmd),
                fun("轨道线路客运强度", BasicRailwayVO::getGdxlkyqq),
                fun("轨道万人车站拥有率", BasicRailwayVO::getGdwrczyyl),
                fun("轨道万人线网拥有率", BasicRailwayVO::getGdwrxwyyl),

                fun("去年铁路路网密度", BasicRailwayVO::getQntllwmd),
                fun("去年高速铁路路网密度", BasicRailwayVO::getQngstllwmd),
                fun("去年普速铁路路网密度", BasicRailwayVO::getQnpstllwmd),
                fun("去年铁路复线率", BasicRailwayVO::getQntlfxRate),
                fun("去年铁路电气化率", BasicRailwayVO::getQntldqhRate)

        ));

        BigDecimal divide = IndustryIndexVo.divide(basicRailwayVO.getTlyxaqhjzlljzz(), basicRailwayVO.getTlyxaqhjzlljpc(), 0, RoundingMode.HALF_UP);

        basicRailwayVO.setBnzgl(new IndustryIndexVo("本年整治率", divide.toString(), "%"));

        BigDecimal divideL = IndustryIndexVo.divide(basicRailwayVO.getQntlyxaqhjzlljzz(), basicRailwayVO.getQntlyxaqhjzlljpc(), 0, RoundingMode.HALF_UP);

        basicRailwayVO.setQnzgl(new IndustryIndexVo("去年整治率", divideL.toString(), "%"));

        //轨道运营计划开行列次
        String gdyyjhkxlc = basicRailwayVO.getGdyyjhkxlc().getIndicatorValue();
        // 解析 JSON 字符串
        JSONObject jsonObject = JSONObject.parseObject(gdyyjhkxlc);
        // 最大值
        BigDecimal maxValue = BigDecimal.ZERO;
        for (String key : jsonObject.keySet()) {
            int i = jsonObject.getBigDecimal(key).compareTo(maxValue);
            if (i > 0) {
                maxValue = jsonObject.getBigDecimal(key);
            }
        }
        basicRailwayVO.getGdyyjhkxlc().setIndicatorValue(maxValue.toString());

        //轨道线路客运强度
        String indicatorValue = basicRailwayVO.getGdxlkyqq().getIndicatorValue();
        // 解析 JSON 字符串
        JSONObject JS = JSONObject.parseObject(indicatorValue);
        // 找出最大值
        BigDecimal max = BigDecimal.ZERO;
        for (String key : JS.keySet()) {

            int i = JS.getBigDecimal(key).compareTo(max);
            if (i > 0) {
                max = JS.getBigDecimal(key);
            }
        }
        basicRailwayVO.getGdxlkyqq().setIndicatorValue(max.toString());


        return basicRailwayVO;
    }


    public MileagePopUpVo mileagePopUp() {
        MileagePopUpVo mileagePopUpVo = new MileagePopUpVo();
        industryIndexService.assembly(mileagePopUpVo, false, CollUtil.toList(
                fun("高速铁路里程", MileagePopUpVo::getGstllc),
                fun("普速铁路里程", MileagePopUpVo::getPstllc),

                fun("去年国家铁路里程", MileagePopUpVo::getGjtllc),
                fun("去年地方铁路里程", MileagePopUpVo::getDftllc),

                fun("去年客运专线里程", MileagePopUpVo::getKyzxlc),
                fun("去年货运专线里程", MileagePopUpVo::getHyzxlc),
                fun("去年客货共线里程", MileagePopUpVo::getKhgxlc),

                fun("去年铁路复线里程", MileagePopUpVo::getTlfxlc),
                fun("去年铁路电气化里程", MileagePopUpVo::getTldqhlc),
                fun("去年铁路复线率", MileagePopUpVo::getTlfxl),
                fun("去年铁路电气化率", MileagePopUpVo::getTldqhl),
                fun("去年高速铁路路网密度", MileagePopUpVo::getQngstllwmd),
                fun("去年普速铁路路网密度", MileagePopUpVo::getQnpstllwmd)
        ));


        BigDecimal Gstlzb = IndustryIndexVo.divide(mileagePopUpVo.getGstllc(), 2, RoundingMode.HALF_UP, mileagePopUpVo.getPstllc(),mileagePopUpVo.getGstllc());
        BigDecimal Pstlzb = IndustryIndexVo.divide(mileagePopUpVo.getPstllc(), 2, RoundingMode.HALF_UP, mileagePopUpVo.getGstllc(),mileagePopUpVo.getPstllc());
        mileagePopUpVo.setGstllczb(new IndustryIndexVo("高速铁路里程占比", Gstlzb.toString(), "%"));
        mileagePopUpVo.setPstllczb(new IndustryIndexVo("普速铁路里程占比", Pstlzb.toString(), "%"));

        BigDecimal Gjtllczb = IndustryIndexVo.divide(mileagePopUpVo.getGjtllc(),  2, RoundingMode.HALF_UP,mileagePopUpVo.getGjtllc(), mileagePopUpVo.getDftllc());
        BigDecimal Dftllczb = IndustryIndexVo.divide(mileagePopUpVo.getDftllc(),  2, RoundingMode.HALF_UP,mileagePopUpVo.getDftllc(), mileagePopUpVo.getGjtllc());
        mileagePopUpVo.setGjtllczb(new IndustryIndexVo("国家铁路里程占比", Gjtllczb.toString(), "%"));
        mileagePopUpVo.setDftllczb(new IndustryIndexVo("地方铁路里程占比", Dftllczb.toString(), "%"));

        BigDecimal Kyzxlczb = IndustryIndexVo.divide(mileagePopUpVo.getKyzxlc(), 2, RoundingMode.HALF_UP,mileagePopUpVo.getKyzxlc(), mileagePopUpVo.getHyzxlc(), mileagePopUpVo.getKhgxlc());
        BigDecimal Hyzxlczb = IndustryIndexVo.divide(mileagePopUpVo.getHyzxlc(), 2, RoundingMode.HALF_UP,mileagePopUpVo.getHyzxlc(), mileagePopUpVo.getKhgxlc(), mileagePopUpVo.getKyzxlc());
        BigDecimal Khgxlczb = IndustryIndexVo.divide(mileagePopUpVo.getKhgxlc(), 2, RoundingMode.HALF_UP,mileagePopUpVo.getKhgxlc(), mileagePopUpVo.getHyzxlc(), mileagePopUpVo.getKyzxlc());
        mileagePopUpVo.setKyzxlczb(new IndustryIndexVo("客运专线里程占比", Kyzxlczb.toString(), "%"));
        mileagePopUpVo.setHyzxlczb(new IndustryIndexVo("货运专线里程占比", Hyzxlczb.toString(), "%"));
        mileagePopUpVo.setKhgxlczb(new IndustryIndexVo("客货共线里程占比", Khgxlczb.toString(), "%"));


        return mileagePopUpVo;
    }
}
